package java_0500_collection;

import java.util.HashSet;
import java.util.Objects;

@SuppressWarnings({"all"})
public class CollectionHomework {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        Person p1 = new Person(1001, "AA");
        Person p2 = new Person(1002, "BB");
        hashSet.add(p1);
        hashSet.add(p2);

        p1.name = "CC";
        hashSet.remove(p1);
        System.out.println(hashSet);
//[java_0500_collection.Person@1076b, java_0500_collection.Person@10b4a]

        hashSet.add(new Person(1001, "CC"));
        System.out.println(hashSet);
//[java_0500_collection.Person@1076b, java_0500_collection.Person@10b4a,
// java_0500_collection.Person@10b4a]

        hashSet.add(new Person(1001, "AA"));
        System.out.println(hashSet);
//[java_0500_collection.Person@1076b, java_0500_collection.Person@10b4a,
// java_0500_collection.Person@10b4a, java_0500_collection.Person@1038a]
    }
}

class Person {
    public String name;
    public int id;

    public Person(int id, String name) {
        this.name = name;
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return id == person.id && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id);
    }
}